const fp = require('lodash/fp');
const cars = [
    {
        name: 'Ferrari FF', horsepower: 660, dollar_value: 700000, in_stock: true
    },
    {
        name: 'Spyker C12 Zagato', horsepower: 650, dollar_value: 648000, in_stock: false
    },
    {
        name: 'Jaguar XKR-S', horsepower: 550, dollar_value: 132000, in_stock: false
    },
    {
        name: 'Audi R8', horsepower: 525, dollar_value: 114200, in_stock: false
    },
    {
        name: 'Aston Martin One-77', horsepower: 750, dollar_value: 1850000, in_stock: true
    },
    {
        name: 'Pagani Huayra', horsepower: 700, dollar_value: 1300000, in_stock: false
    }
]
//1.使用函数组合fp.flowRight()重新实现下面这个函数

const last = arr => fp.last(arr)
const in_stock = s => fp.prop('in_stock',s)
const f = fp.flowRight(in_stock,last);
// console.log(f(cars))

//2. 使用fp.flowRight()、fp.prop()和fp.first()获取第一个car的name
const first = arr => fp.first(arr)
const name = s => fp.prop('name',s)
const g = fp.flowRight(name,first)
// console.log(g(cars))

//3. 使用帮助函数_average 重构averageDollarValue,使用函数组合的方式实现
// let _average = function(xs){
//     return fp.reduce(fp.add,0,xs)/xs.length
// }
// let averageDollarValue = function(cars){
//     let dollar_values = fp.map(function(car){
//         return car.dollar_value
//     },cars)
//     return _average(dollar_values)
// }
// console.log(_average(cars))
const h = cars => fp.map(item => item.dollar_value,cars)
const newAverageDollarValue = fp.flowRight(_average,h)
// console.log(newAverageDollarValue(cars))

//4.把数组中的name转换为这种形式：
// sannizeNames(["Hello World"]) => ["hello_world"]
let _underscore = fp.replace(/\w+/g,'_')
// const sannizeNames = fp.flowRight(fp.map(fp.))
// console.log(sannizeNames["Hello World"])